<template>
  <div>
    <el-form :inline="true">
      <el-form-item label="活动名称">
        <el-input v-model="sous" style="width:300px"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="mini">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button size="mini">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" size="mini" @click="xinzeng(0,'无')">新增分类</el-button>
    <el-table :data="list" style="width: 100%;margin-bottom: 20px;" row-key="id" border default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column prop="name" label="分类名称">
      </el-table-column>
      <el-table-column prop="course_num" label="课程数量">
      </el-table-column>
      <el-table-column prop="sort" label="序号">
      </el-table-column>
      <el-table-column prop="address" label="操作">
        <template slot-scope="scope">
          <el-button @click="xinzeng(scope.row.id,scope.row.label)" type="text" size="small"
            v-if="scope.row.parent_id==0">新增
          </el-button>
          <el-button type="text" size="small" @click="edit(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="remove(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹出层 -->
    <el-dialog title="添加/修改" :visible.sync="show" width="30%">
      <el-form>
        <el-form-item label="所属分类" label-width="120px">
          {{font}}
        </el-form-item>
        <el-form-item label="分类名称" label-width="120px">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="排序" label-width="120px">
          <el-input-number v-model="form.sort" label="描述文字"></el-input-number>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="quxiao">取 消</el-button>
        <el-button type="primary" @click="yes">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      font: "无",
      list: [],
      show: false,
      sous: "",
      uid: 0,
      form: {
        parent_id: 0,
        sort: 1,
        title: "",
      },
    };
  },
  mounted() {
    axios.get("/course/classify").then((res) => {
      console.log(res);
      this.list = res.data;
    });
  },
  methods: {
    quxiao() {
      this.form.title = "";
      this.show = false;
    },
    edit(row) {
      console.log(row);
      this.show = true;
      this.uid = row.id;
      this.form.parent_id = row.parent_id;
      this.form.title = row.label;
      this.form.sort = row.sort;
    },
    xinzeng(id, title) {
      this.form.parent_id = id;
      this.font = title;
      this.show = true;
    },
    remove(id) {
      axios.delete("/course/classify/" + id).then((res) => {
        if (res) {
          axios.get("/course/classify").then((res) => {
            console.log(res);
            this.list = res.data;
          });
        }
      });
    },
    yes() {
      if (this.uid == 0) {
        axios
          .post("/course/classify", this.form)
          .then((res) => {
            if (res) {
              //如果添加成功了，就调用接口 重新渲染数据
              axios.get("/course/classify").then((res) => {
                console.log(res);
                this.list = res.data;
                this.$message.success("操作成功");
                this.form.title = "";
              });
            }
          })
          .finally(() => {
            this.show = false;
          });
      } else {
        axios.put("/course/classify/" + this.uid, this.form).then((res) => {
          if (res) {
            axios.get("/course/classify").then((res) => {
              console.log(res);
              this.list = res.data;
              this.show = false;
            });
          }
        });
        this.form.title = "";
      }
    },
  },
};
</script>

<style lang='scss'>
</style>
